#ifndef _GSMAPI_H_
#define _GSMAPI_H_

#include "nano_db.h"
#include "syscore.h"
#include "apis.h"
#include "network_frame.h"
#include "../logServer/clogger.h"
#define GSMLogDebug(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_DEBUG, fmt, ##args);
#define GSMLogInfo(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_INFO, fmt, ##args);
#define GSMLogNotice(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_NOTICE, fmt, ##args);
#define GSMLogWarning(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_WARNING, fmt, ##args);
#define GSMLogError(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_ERR, fmt, ##args);
#define GSMLogCrit(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_CRIT, fmt, ##args);
#define GSMLogAlert(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_ALERT, fmt, ##args);
#define GSMLogEmerg(fmt, args...) CLogger(MODULE_GSM_CAP, __FILE__, __LINE__, LOG_EMERG, fmt, ##args);
#ifdef __cplusplus
extern "C" {
#endif
 
    
#define GSM_BOARD_IP                   "192.168.178.202"
#define GSM_BOARD_UDP_PORT             "5558"
#define GSM_MODULE_IP                  "192.168.178.111"
#define GSM_MODULE_UDP_PORT            "5557"
/**
 *****************************************************************************************
 * Message ID between gsm baseband card and gsm module
 * the same for request and response
 *
 *****************************************************************************************
 */
#define GSM_BASEBAND_CFGSEARCH         0x01
#define GSM_BASEBAND_CFGRESET          0X02
#define GSM_BASEBAND_START             0X05
#define GSM_BASEBAND_STOP              0X06
#define GSM_BASEBAND_RSTSYS            0X09
#define GSM_BASEBAND_FREQUECY_SCAN     0x03
#define GSM_BASEBAND_SET_NEIGHBOR      0x04  
#define GSM_BASEBAND_RESTART           0x07  
#define GSM_BASEBAND_FILE_READ         0x08
#define GSM_BASEBAND_UPGRADE           0x0c

#define GSM_BASEBAND_SET_TXGAIN        0x23
#define GSM_BASEBAND_SET_RXGAIN        0x24
#define GSM_BASEBAND_COLLECT_UEID      0x21



/***
 *****************************************************************************************
 * Message ID between gsm baseband card and gsm module
 * the response of request
 *
 *****************************************************************************************
 */

#define GSM_BASEBAND_RSP_CFGSEARCH         0x11
#define GSM_BASEBAND_RSP_CFGRESET          0X12
#define GSM_BASEBAND_RSP_FREQUECY_SCAN     0X13
#define GSM_BASEBAND_RSP_START             0X15
#define GSM_BASEBAND_RSP_STOP              0X16
#define GSM_BASEBAND_RSP_RSTSYS            0X19
#define GSM_BASEBAND_UPLOAD                0x18
#define GSM_BASEBAND_HEART_BEAT            0x1F
#define GSM_BASEBAND_SCAN_RESULT           0x1E
#define GSM_BASEBAND_RSP_UPGRADE           0x1c
#define GSM_BASEBAND_LOG_REPORT			   0x3F

/***
 *****************************************************************************************
 * MESSAGE paraType of the baseband
 * 
 *
 *****************************************************************************************
 */

#define OMC_PARAM_SOFTVER	0x0001
#define OMC_PARAM_NTPSRV	0x0003
#define OMC_PARAM_RUNTIME	0x0004
#define OMC_PARAM_DEVTIME	0x0005
#define OMC_PARAM_MCC		0x0101
#define OMC_PARAM_MNC		0x0102
#define OMC_PARAM_LAC		0x0103
#define OMC_PARAM_CI		0x0104
#define OMC_PARAM_CFGMODE	0x010A
#define OMC_PARAM_WORKMODE	0x010B
#define OMC_PARAM_CAPTIME	0x010C
#define OMC_PARAM_FTP_ADDR  0x0040
#define OMC_PARAM_FTP_PORT  0x0041
#define OMC_PARAM_UPGRADE_ADDR  0x0042
#define OMC_PARAM_UPGRADE_FILE_NAME 0x0043
#define OMC_PARAM_FTP_USER   0x004A
#define OMC_PARAM_FTP_PWD    0x004B
#define OMC_PARAM_UPGRADE_TYPE  0x009A
#define OMC_PARAM_UPGRADE_MD5   0x009B
#define OMC_PARAM_UPGRADE_RSP   0x009F
#define OMC_PARAM_CELL_CRO      0X0106



#define OMC_PARAM_FREQ		0x0150
#define OMC_PARAM_DNATT		0x0151
#define OMC_PARAM_UPATT		0x0152

#define OMC_PARAM_900_START_FREQ    0x0156
#define OMC_PARAM_900_END_FREQ      0x0157
#define OMC_PARAM_1800_START_FREQ   0x0158
#define OMC_PARAM_1800_END_FREQ     0x0159
#define OMC_PARAM_FREQ_OFFSET       0x015f

#define OMC_PARAM_CARRIER   0x0200
#define OMC_PARAM_IMSI		0x0211
#define OMC_PARAM_IMEI		0x0212
#define OMC_PARAM_SIGQUA	0x0215

#define GSM_SLOT_1                     8
#define GSM_SLOT_2                     9
    
#define GSM_CARRIER_INDEX_0            0
#define GSM_CARRIER_INDEX_1            1
#define GSM_CARRIER_INDEX_BOTH         2

#define GSM_PARITY_MASK                0xFF
#define GSM_CARRIER_CMCC               1
#define GSM_CARRIER_UNICOM             2

#define TICK_INTERVAL                  1000
#define TICK_SECOND                    (TICK_INTERVAL / 1000)    

#define INVALID_CMD_CODE               0xffff
#define INVALID_SRC_ID                 0xffff

#define UE_ID_LENGTH                   16 

#define AUTOBCCHMODE_1                 1 // weakest 
#define AUTOBCCHMODE_2                 2 //
#define AUTOBCCHMODE_3                 3 // 
#define AUTOBCCHMODE_4                 4 //
#define AUTOBCCHMODE_5                 5 // 
#define NUM_OF_NEIGHBOR                100    

#define GSM_CONFIG_MODE_AUTO           0
#define GSM_CONFIG_MODE_MANUAL         1


typedef enum {
    GSM_MODULE_IDLE,
    GSM_MODULE_RESTART,	
    GSM_MODULE_SHUTDOWN,
    GSM_MODULE_NSYNC,
	GSM_MODULE_PRECONFIG,    
	GSM_MODULE_CONFIGING,
    GSM_MODULE_CONFIGED,
    GSM_MODULE_RUNNING,
} gsmModuleState_t;

typedef enum {
    GSM_MODULE_GSM_BASEBAND_IDLE,
    GSM_MODULE_GSM_BASEBAND_TIME_CONFIGED,          //After configuration 
    GSM_MODULE_GSM_BASEBAND_CARRIER_CONFIGED,       //After configuration 
    GSM_MODULE_GSM_BASEBAND_WAIT_RESPONSE,          //wait for response
    GSM_MODULE_GSM_BASEBAND_RESTARTING,             //restarting 
    GSM_MODULE_GSM_BASEBAND_SNIFFER_SHUTDOWN,       //shutdown for sniffer
    GSM_MODULE_GSM_BASEBAND_SHUTDOWN,               //shutdown
    GSM_MODULE_GSM_BASEBAND_RUNNING,                //running    
    GSM_MODULE_GSM_BASEBAND_NSYNC                   //GSM baseband out of sync, no heat beat
} gsmBasebandState_t;

typedef struct
{
    U32   paramType;
    U32   paramLength;
}paramCode_t;

#define PARAM_CODE_LENGTH sizeof(paramCode_t)

typedef struct
{
    U32   frequency;
    U32   mcc;
    U32   mnc;
    U32   lac;
    U32   band;
    U32   carrier;
    U32   cro; 
    S32   rssiMin;
    U32   thresholdDelta;  
    U32   captime;
    U32   dnatt;
    U32   upatt;
    U32   cnum;
    U32   configMode;
    U32   workMode;  
    U32   startFreq900;
    U32   endFreq900;
    U32   startFreq1800;
    U32   endFreq1800;
    U32   freqOffset; 
}geranParam_t;

typedef struct
{
    U8 autoBccMode;
    U8 isAuto;
    U8 hour;
    U8 minutes;
    U8 second;
    U8 snifferInterval;
    U8 reverved[2];
}snifferParam_t;

typedef struct
{
    U32                     duration;              //duration for not capture the same ue id
    snifferParam_t          snifferParam;          //parameters for sniffer
    bool                    cellEnable[2];          //true:enable, false: disable
    bool                    rfEnable[2];            //true:enable, false: disable
    geranParam_t            geranParam[2];         //parameters for two carrier
    sockaddr_in             remoteAddr;            //gsm baseband ip address and udp port
    sockaddr_in             localAddr;             //local ip address and udp port
}configurationParam_t;

/**
 *****************************************************************************************
 * Message structure between gsm baseband card and gsm module
 *
 *****************************************************************************************
 */
#define  MAX_NGHRCELL_NUM  1024 
typedef struct cellItemTag
{
   U16	wARFCN;
   U16	wBSIC;
   S8	cRSSI;
   U8	bReserved;
   U8	cC1;
   U8	bC2;
}cellItem;
	
typedef struct neighboringCellInfoTag
{
   U32		  bGlobalCellId;
   U8		  bPLMNId[5];
   S8		  cRSSI;
   U16		  wLAC;
   U16		  wBSIC;
   U16		  wARFCN;
   U8		  cC1;
   U8		  bNbCellNum;				  
   U8		  bReserved;
   U8		  bC2;
   cellItem   stNbCell[32];
}neighboringCellInfo;

typedef struct cellResultTag
{
   U16		  wLAC;   
   U16	      wARFCN;
   U16        wBSIC;
   S8	      cRSSI;
   S8	      cC1;
   U8	      bC2;
}cellResult;

typedef struct snifferResultTag
{
   U32		    cellNum;
   U8		    bPLMNId[8];   
   cellResult   cell[MAX_NGHRCELL_NUM];
}snifferResult;

typedef struct totalSnifferResultTag
{
	U32 		          number;
    neighboringCellInfo   result[100];
}totalSnifferResult;

/**
 *****************************************************************************************
 * Message structure between gsm baseband card and gsm module
 *
 *****************************************************************************************
 */
#define GSM_GENERAL_HEADER_LENGTH   sizeof(gsmBasebandGeneralMsg_t) 
typedef struct
{
    U32 length;
    U8  msgIndex    ;      
    U8  msgId       ;   
    U8  carrierIdx  ;   /* 0: for carrier 0 and 1 : for carrier 1 */ 
    U8  rsFiled     ;   

}gsmBasebandGeneralMsg_t;

#pragma pack(1)
typedef struct PARM{
    U8 paraLength;
    U16 paraType; 
    U8  paraValue[0];   
}MSGPARAM,*MSGPPARAM;
#pragma pack()

typedef struct
{
     U16           srcID;
     CMDHEADER     cmdHdr;
}opRecord_t;

typedef struct
{
    U16                             selfId;          /* module ID */	
    U16                             hbCounter;       /* count how many heart beat message received */
    U16                             resetCount;
    U16                             timeTick;        /* time Tick for out sync judgement */
    U16                             timeOutThresh;   /* time out threshold */
    U32                             snifferTick;     /* count tick for sniffer */    
    U32                             rspTick[2];      /* rsp tick for waiting response */
    opRecord_t                      record[2];       /* save destination module ID and cmd code */
                                                     /* for response     */
    conn_context                    udpCtxt;         /* udp connection context */
    conn_t                          *udp_conn;        /* udp connection context for gsm module and baseband */
    configurationParam_t            config;
    struct ndb_t                    ndb;             /* keep configuration database */
    gsmModuleState_t                state;           /* gsm module state */
    gsmBasebandState_t              gsmPreState[2];  /* gsm baseband current state */
    gsmBasebandState_t              gsmCurState[2];  /* gsm baseband previous state */      
    pthread_mutex_t                 mut;             /* for modify gsm baseband state between two thread */
    U8                              msg[256];        /* the lastest udp message */
    U8                              msglength;       /* the lastest udp message length */
    time_t                          curTime;         /* current time counter since from 1970 */
    
    U32                             sendcout[RC_MOD_INVALID];   /* send ok */
    bool                            isSniffering;               /* flag for sniffering */
    U32                             totalNbr;                   /* total neighboring cell */
    U32                             numCmcc;                    /* cmcc neighboring cell */
    U32                             numUnicom;                  /* unicom neighboring cell */
}gsmModuleContext_t;

extern gsmModuleContext_t  g_GsmModuleCntx;

#ifdef __cplusplus
}
#endif

#endif
